package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.PowerRoomService;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PowerRommServiceImpl implements PowerRoomService {
    @Autowired
    private PowerRoomMapper powerRoomMapper;
    @Autowired
    private NavbarMapper navbarMapper;
    @Autowired
    private OrderDeliveryMenuMapper orderDeliveryMenuMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageInfo<PowerRoom> selectByConditions(Integer pageNum, Integer pageSize) {
        List<PowerRoom> list = powerRoomMapper.selectByConditions(pageNum,pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public int addPowerRoom(String roomName,String address,String areaId,String projectId,Integer highVoltageBoard,Integer lowPressureTank) {
        List<City> list=cityMapper.queryCity();
        Project pro=projectMapper.queryProjectByProId(projectId);
        PowerRoom powerroom=new PowerRoom();
        powerroom.setProjectName(pro.getProjectName());
        powerroom.setRoomName(roomName);
        powerroom.setAddress(address);
        powerroom.setAreaId(areaId);
        powerroom.setProjectId(projectId);
        powerroom.setHighVoltageBoard(highVoltageBoard);
        powerroom.setLowPressureTank(lowPressureTank);
        for (City citys : list) {
            if(citys.getCityId().equals(areaId)){
                powerroom.setAreaName(citys.getCityName());
            }
        }
        return powerRoomMapper.insert(powerroom);
/*        Navbar navbar = navbarMapper.selectNavbarByNavId(navId);
        PowerRoom powerRoom = new PowerRoom();
        powerRoom.setAreaId(navbar.getpId().toString());
        Navbar navbar1 = navbarMapper.selectNavbarById(navbar.getpId());
        powerRoom.setAreaIds(navbar1.getNavbarId());
        powerRoom.setProjectId(navId);
        powerRoom.setRoomId(IdUtil.getRandomString(8));
        powerRoom.setRoomName(roomName);
        powerRoom.setAreaName(navbar.getpName());
        powerRoom.setProjectName(navbar.getNavName());
        powerRoom.setAddress(address);
        powerRoomMapper.insert(powerRoom);
        OrderDeliveryMenu orderDeliveryMenu1 = orderDeliveryMenuMapper.getOrderDeliveryMenuByNavId(navbar.getNavbarId());
        OrderDeliveryMenu orderDeliveryMenu = new OrderDeliveryMenu();
        orderDeliveryMenu.setName(roomName);
        orderDeliveryMenu.setNavId(powerRoom.getRoomId());
        orderDeliveryMenu.setpId(orderDeliveryMenu1.getNavId());
        orderDeliveryMenu.setpName(navbar.getNavName());
        return orderDeliveryMenuMapper.insert(orderDeliveryMenu);*/


    }

    @Override
    public int delPowerRoom(Integer id) {
            return powerRoomMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updatePowerRoomById(Integer id,String roomName, String address,String highVoltageBoard,String lowPressureTank) {
        return powerRoomMapper.updatePowerRoomById(id, roomName, address,highVoltageBoard,lowPressureTank);
    }

    @Override
    public PowerRoom getAllById(Integer id) {
        return powerRoomMapper.selectById(id);
    }

    @Override
    public List<PowerRoom> selectNavbarByNavId(String projectId) {
        return powerRoomMapper.selectNavbarByNavId(projectId);
    }

    @Override
    public List<PowerRoom> selectByAreaId(String cityId) {
        return powerRoomMapper.selectByAreaId(cityId);
    }

    @Override
    public List<PowerRoom> all() {
        return powerRoomMapper.all();
    }

    /**
     * 根据项目Id查询配电房
    * */
    @Override
    public List<PowerRoom> queryPowerroom(String projectId) {
        return powerRoomMapper.queryPowerroom(projectId);
    }

    /**
     * 根据项目Id查询配电房
     * */
    @Override
    public PageInfo<PowerRoom> selectByCityId(Integer pageNum, Integer pageSize,String cityId, String projectId ) {
        List<PowerRoom> list = powerRoomMapper.selectByCityId(pageNum,pageSize,cityId,projectId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<PowerRoom> selectByTokenId(Integer pageNum, Integer pageSize, String cityId, String projectId, String token) {
        List<PowerRoom> list = powerRoomMapper.selectByTokenId(pageNum,pageSize,cityId,projectId,token);
        return new PageInfo<>(list);
    }

    @Override
    public int selectCountByProjectId(String projectId) {
        return powerRoomMapper.selectCountByProjectId(projectId);
    }

/*    @Override
    public List<PowerRoom> queryPowerroomByToken(String projectId) {
        return powerRoomMapper.queryPowerroomByToken(projectId);
    }*/

    /*
    * 模糊查询
    * */
    @Override
    public PageInfo<PowerRoom> queryPowerRoomLike(Integer pageNum,Integer pageSize,String text, String token) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null){
            if(sysUser.getStatus()==2) {
                return new PageInfo<>(powerRoomMapper.queryPowerRoomLike(pageNum,pageSize,text,sysUser.getProjectId().toString()));
            }
        }
        return  new PageInfo<>(powerRoomMapper.queryPowerRoomLike(pageNum,pageSize,text,null));
    }
}
